package com.itheima.tlias84.config;

import com.aliyun.oss.OSS;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 阿里云Oss客户端
 */
@Component
@Configuration
public class OssClient {

    @Resource
    OSS ossBean1;

    @Resource
    AliyunOssConfig ossConfig;

    /**
     * 阿里云上传图片方法
     * @param fileName 文件名
     * @param inputStream 文件输入流对象
     * @return上传之后返回的访问阿里云oss的Url地址
     */
    public String upload(String fileName, InputStream inputStream){

        int i = fileName.lastIndexOf(".");
        String suffix = fileName.substring(i);
        String uuid = UUID.randomUUID().toString();
        String objectName = uuid+suffix;


        ossBean1.putObject(ossConfig.getBucketName(),objectName,inputStream);
        String url = ossConfig.getUrl()+objectName;
        return url;
    }
}
